package cn.com.nes.site.controller.workOrder;

import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.vo.workOrder.FaultWorkOrderVo;
import cn.com.nes.site.entity.vo.workOrder.RepairWorkOrderVo;
import cn.com.nes.site.module.service.monitor.EquipmentInfoService;
import cn.com.nes.site.service.system.user.SystemUserService;
import cn.com.nes.site.service.workOrder.RepairWorkOrderService;
import cn.com.nes.site.service.workOrder.dto.RepairWorkOrderDetailDto;
import cn.com.nes.site.service.workOrder.dto.RepairWorkOrderDto;
import cn.com.nes.site.service.workOrder.dto.WorkOrderFollowUpRecordDto;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 客户服务 - 维修工单
 *
 * @Author: ShenWei
 * @Date: 2024/03/07/14:42
 */
@Deprecated
@Controller
@RequestMapping("/v2/workOrder/repairWorkOrder")
public class RepairWorkOrderController {
    private static final String WORK_ORDER_LEVEL_URGENT = "紧急"; // 工单等级
    private static final String WORK_ORDER_TYPE_REPORT = "故障保修"; // 工单类型

    @Autowired
    RepairWorkOrderService repairWorkOrderService;

    @Autowired
    EquipmentInfoService equipmentInfoService;

    @Autowired
    SystemUserService systemUserService;

    /**
     * 查询维修工单列表
     */
    @RequestMapping(value = "/getRepairWorkOrderInfoList",method = RequestMethod.GET)
    @ResponseBody
    public PageData getRepairWorkOrderInfoList(RepairWorkOrderVo vo) throws Exception {
        convertTimeFormat(vo);
        vo.setUserId(vo.getUserInfo().getId());
        vo.setManufactureId(vo.getUserInfo().getNes_companyId());
        vo.setDepartmentId(vo.getUserInfo().getDepartmentId());

        // 设置工单主管id
        if(StrUtil.isNotBlank(vo.getOrderDirectorId())){
            String orderDirectorId = vo.getUserInfo().getId();
            vo.setOrderDirectorId(orderDirectorId);
        }
        if (StrUtil.isNotBlank(vo.getUserInfo().getStationId())) {
            vo.setDataRange(Byte.valueOf(vo.getUserInfo().getDataRange()));
        }
        vo.setDataRange(Byte.valueOf(vo.getUserInfo().getDataRange()));//这里不设置数据范围，默认按照区域查看
        // 设置状态参数
        if (StrUtil.isNotBlank(vo.getWorkOrderState())){
            ArrayList<Integer> statusList = new ArrayList<>();
            statusList.add(Integer.valueOf(vo.getWorkOrderState()));
            vo.setWorkOrderStatusList(statusList);
        }
        //维修工单-查询列表或单条
        return repairWorkOrderService.getRepairWorkOrderInfoList(vo);
    }

    // 转换时间格式
    private void convertTimeFormat(RepairWorkOrderVo vo) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StrUtil.isNotBlank(vo.getCreateTimeStartStr())){
            vo.setCreateTimeStart(format.parse(vo.getCreateTimeStartStr()));
        }
        if (StrUtil.isNotBlank(vo.getCreateTimeEndStr())){
            vo.setCreateTimeEnd(format.parse(vo.getCreateTimeEndStr()));
        }
        if (StrUtil.isNotBlank(vo.getCompleteTimeStartStr())){
            vo.setCompleteTimeStart(format.parse(vo.getCompleteTimeStartStr()));
        }
        if (StrUtil.isNotBlank(vo.getCompleteTimeEndStr())){
            vo.setCompleteTimeEnd(format.parse(vo.getCompleteTimeEndStr()));
        }
    }

    /**
     * 查询工单详情
     */
    @RequestMapping(value = "/getRepairWorkOrderDetail",method = RequestMethod.GET)
    @ResponseBody
    public PageData getRepairWorkOrderDetail(RepairWorkOrderVo vo) throws Exception {
        RepairWorkOrderDetailDto detail = new RepairWorkOrderDetailDto();
        String workOrderId = vo.getWorkOrderId();
        if (StrUtil.isBlank(workOrderId)) {
            return PageData.error("请传入参数:workOrderId");
        }
        // 1、工单跟踪记录
        List<WorkOrderFollowUpRecordDto> followList = repairWorkOrderService.getWorkOrderFollowUpRecordsById(workOrderId);
        detail.setFollowList(followList);
        // 2、工单基础信息
        RepairWorkOrderDto workOrderDto = repairWorkOrderService.getRepairWorkOrderDetail(vo);
        detail.setWorkOrderDto(workOrderDto);
        // 3、工程图片
        // 4、处理过程
        // 5、维修信息
        // 6、回访信息

        Page<RepairWorkOrderDetailDto> page = new Page<>(1, 1);
        List<RepairWorkOrderDetailDto> list = new ArrayList<>();
        list.add(detail);
        return PageData.ok(page,list);
    }

    /**
     * 创建工单
     */
    @RequestMapping(value = "/addWorkOrder",method = RequestMethod.POST)
    public PageData addWorkOrder(@RequestBody RepairWorkOrderVo vo) throws Exception {
        String userId = vo.getUserInfo().getId();
        String manufactureId = vo.getUserInfo().getNes_companyId(); // 厂商id
        Byte isAgent = systemUserService.getIsAgent(vo.getUserInfo().getId()); // 是否代理商 0-不是代理商 1-是代理商

        //如果需要派单审核，则派单负责人不能为空
        if(StrUtil.isNotBlank(vo.getIsNeedAudit()) && "1".equals(vo.getIsNeedAudit()) && StrUtil.isBlank(vo.getOrderDirectorId())){
            return PageData.error("派单负责人不能为空！");
        }
        // 组装参数
        FaultWorkOrderVo faultWorkOrderVo = new FaultWorkOrderVo();
        faultWorkOrderVo.setUserId(userId);
        faultWorkOrderVo.setManufactureId(manufactureId);
        faultWorkOrderVo.setReportTime(new Date());
        faultWorkOrderVo.setProcessTimeLimit(24);
        faultWorkOrderVo.setIfSendSms(0);
        faultWorkOrderVo.setWorkOrderLevel(WORK_ORDER_LEVEL_URGENT);
        faultWorkOrderVo.setWordOrderType(WORK_ORDER_TYPE_REPORT);
        faultWorkOrderVo.setAddress(vo.getAddress());
        faultWorkOrderVo.setContact(vo.getContactName());
        faultWorkOrderVo.setLocationId(vo.getLocationId());
        faultWorkOrderVo.setPhone(vo.getPhone());
        faultWorkOrderVo.setEquipmentId(vo.getEquipmentId());
        faultWorkOrderVo.setAreaCode(vo.getAreaCode());
        faultWorkOrderVo.setFaultInfo(vo.getFaultInfo());
        faultWorkOrderVo.setDistance(vo.getDistance());
        faultWorkOrderVo.setOrderDirectorId(vo.getOrderDirectorId());
        //如果前端不传工单来源，则默认传入4;  -->  迁移代码,不知道为啥是4
        if (null != vo.getWorkOrderSource()) {
            faultWorkOrderVo.setWorkOrderSource(vo.getWorkOrderSource());
        }else{
            faultWorkOrderVo.setWorkOrderSource(4);
        }
        if (StrUtil.isNotBlank(vo.getMaintenanceWorkerId())) {
            faultWorkOrderVo.setMaintainId(vo.getMaintenanceWorkerId());
            faultWorkOrderVo.setMaintainPhone(vo.getMaintenanceWorkerPhone());
        }
        //如果传入工单状态
        boolean flag = vo.getReportWorkOrder() == 1;
        if (flag) {
            faultWorkOrderVo.setMaintainId(userId);
        }
        String workOrderId = repairWorkOrderService.addFaultWorkOrder(faultWorkOrderVo);
        if (flag) {
            repairWorkOrderService.updateReportWorkOrder(workOrderId);
        }

        // 返回工单id
        HashMap<String, Object> map = new HashMap<>();
        map.put("workOrderId", workOrderId);
        return PageData.ok(map);
    }

    /**
     * 绑定设备
     */

}
